package com.cr.api.controller;

import com.cr.api.vo.NoticeVo;
import com.cr.business.domain.CrNotice;
import com.cr.business.service.ICrNoticeService;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.utils.ConvertUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 案源委托Controller
 * 
 * @author G
 * @date 2021-09-14
 */
@Api(tags = "公告咨询", value = "公告咨询")
@RestController
@CrossOrigin
@RequestMapping("/api/notice")
public class ApiNoticeController extends BaseController
{
    @Autowired
    private ICrNoticeService noticeService;

    /**
     * 公告咨询列表
     */
    @ApiOperation("公告咨询列表")
    @PostMapping("/getNoticeList")
    @ResponseBody
    public AjaxResult getNoticeList()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        CrNotice crNotice = new CrNotice();
        crNotice.setIsHot(0);
        startPage();
        List<CrNotice> noticeList = noticeService.selectCrNoticeList(crNotice);
        List<NoticeVo> noticeListVoList = new ArrayList<>();
        if(noticeList.size() > 0){
            noticeListVoList = ConvertUtil.convertList(noticeList, NoticeVo.class);
            for(NoticeVo vo : noticeListVoList){
                if(vo.getContent().length() > 19){
                    vo.setContent(vo.getContent().substring(0, 20) + "...");
                }
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("noticeList", new PageInfo<>(noticeListVoList));
        return AjaxResult.success(data);
    }

    /**
             * 公告详情
     * @param noticeId
     * @return
     */
    @ApiOperation("公告详情")
    @ApiImplicitParam(name = "noticeId", value = "公告ID", required = true, dataType = "int")
    @PostMapping("/getNoticeInfo")
    @ResponseBody
    private AjaxResult getNoticeInfo(@RequestBody CrNotice param){
        Long noticeId = param.getNoticeId();
        if(noticeId == null){
            return AjaxResult.error("公告id不能为空");
        }
        CrNotice notice = noticeService.selectCrNoticeByNoticeId(noticeId);
        NoticeVo vo = ConvertUtil.convert(notice, NoticeVo.class);
        return AjaxResult.success(vo);
    }
}
